package com.odm.communitymanage.application

import android.app.Application
import android.content.Context
import com.blankj.utilcode.util.LogUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.util.RxHttpManager
//import com.scwang.smart.refresh.header.ClassicsHeader
//import com.scwang.smart.refresh.layout.SmartRefreshLayout
import io.reactivex.plugins.RxJavaPlugins
import me.yokeyword.fragmentation.BuildConfig
import me.yokeyword.fragmentation.Fragmentation


/**
 * @description: Application 实现类
 * @author: ODM
 * @date: 2020/4/10
 */
class App : Application() {

    companion object{
        private lateinit var context : Context
        fun getContext(): Context {
            return context
        }
    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        initFragmentation()
        initLiveEventConfig()
        initRxJavaOnErrorHandle()
        RxHttpManager.init(this)
    }



    private fun initFragmentation() {
        Fragmentation.builder() // 设置 栈视图 模式为 悬浮球模式   SHAKE: 摇一摇唤出   NONE：隐藏
            .stackViewMode(Fragmentation.BUBBLE)
            .debug(BuildConfig.DEBUG) // 在遇到After onSaveInstanceState时，不会抛出异常，会回调到下面的ExceptionHandler
            .handleException{
//                throw it //抛出异常
                LogUtils.e("FragmentationException" , it.localizedMessage)
            }
            .install()
    }

    /**
     * 全局捕获RxJava异常
     */
    private fun initRxJavaOnErrorHandle() {
        RxJavaPlugins.setErrorHandler {
                if(it.localizedMessage != null){
                    LogUtils.e("RxJavaException" ,it.localizedMessage ?: "null error msg");
                }
        }
    }

    private fun initLiveEventConfig() {
        LiveEventBus
            .config() //配置在没有Observer关联的时候是否自动清除LiveEvent以释放内存（默认值false）
            .autoClear(true)
            .lifecycleObserverAlwaysActive(true)
    }

}